//
//  ImageViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "ImageViewController.h"
#import "NetworkHandler.h"
#import "ImageButton.h"
#import "SecondViewController.h"
@interface ImageViewController ()

@property (nonatomic, retain) NSMutableArray *array;
@end

@implementation ImageViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr  = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self imgView];
    
//    [self creearee];
}
//- (void)createScroll
//{
//    UIScrollView *scorll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
//    [scorll setContentSize:CGSizeMake(scorll.frame.size.width, 1000)];
//    [scorll release];
//    
//    [self.view addSubview:scorll];
//    
//}
- (void)imgView
{
    NSString * str =[NSString stringWithFormat:@"http://api.m.mtime.cn/Movie/Image.api?movieId=197464"];
    [NetworkHandler connectioonNetGETDataWithURLString:str completion:^(id result) {
        
        self.arr =(NSArray *)result;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                UIScrollView *scorll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                [scorll setContentSize:CGSizeMake(scorll.frame.size.width, 2000)];
                
                [self.view addSubview:scorll];
                [scorll release];
                for (int i = 0; i < [_arr count]; i++) {
                    ImageButton *aButton = [[ImageButton alloc] initWithFrame:CGRectMake(20 + i % 3 * 100, i / 3 * 100 + 20, 80, 80)];
                    NSDictionary * dic =[_arr objectAtIndex:i];
                    NSLog(@"%@", dic);
                
                NSString *name = [dic objectForKey:@"image"];
                
                NSURL * url =[NSURL URLWithString:name];
                NSData * data =[NSData dataWithContentsOfURL:url];
                
                UIImage * ima=[UIImage imageWithData:data];
                [aButton setBackgroundImage:ima forState:UIControlStateNormal];
                [aButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [scorll addSubview:aButton];
                //[aButton release];
  
            
                }
            });
        
        
            //        [aButton setBackgroundColor:[UIColor redColor]];
        
    }];
    
   

    
}
//
- (void)buttonAction:(id)sender
{
    ImageButton *aButton = (ImageButton *)sender;
    
    SecondViewController *second = [[SecondViewController alloc] init];
    second.imageName = aButton.imageName;
    second.imageArray = self.array;
    [self.navigationController pushViewController:second animated:YES];
    [second release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
